Before working on this assignment please read these instructions fully. In the submission area, you will notice that you can click the link to Preview the Grading for each step of the assignment. This is the criteria that will be used for peer grading. Please familiarize yourself with the criteria before beginning the assignment.
An NOAA dataset has been stored in the file data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv
. The data for this assignment comes from a subset of The National Centers for Environmental Information (NCEI) Daily Global Historical Climatology Network (GHCN-Daily). The GHCN-Daily is comprised of daily climate records from thousands of land surface stations across the globe.
Each row in the assignment datafile corresponds to a single observation.
The following variables are provided to you:
For this assignment, you must:
The data you have been given is near Ann Arbor, Michigan, United States, and the stations the data comes from are shown on the map below.
In [1]:
import matplotlib.pyplot as plt
import mplleaflet
import pandas as pd
import numpy as np
def leaflet_plot_stations(binsize, hashid):
df = pd.read_csv('data/C2A2_data/BinSize_d{}.csv'.format(binsize))
station_locations_by_hash = df[df['hash'] == hashid]
lons = station_locations_by_hash['LONGITUDE'].tolist()
lats = station_locations_by_hash['LATITUDE'].tolist()
plt.figure(figsize=(8,8))
plt.scatter(lons, lats, c='r', alpha=0.7, s=200)
return mplleaflet.display()
leaflet_plot_stations(400,'fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89')
Out[1]:
In [2]:
df=pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv')
# Take a look at the df
df.info()
df.head()
Out[2]:
In [3]:
# We can see above the 'Date' is an object not a datetime, convert below
df['Date'] = pd.to_datetime(df['Date'], yearfirst = True)
df.info()
In [4]:
dfmax = df.pivot_table(index='Date', columns='Element', aggfunc='max')
dfmax.drop('ID', 1, inplace=True)
dfmax.columns = dfmax.columns.droplevel()
dfmax.drop('TMIN', 1, inplace=True)
dfmax.head()
Out[4]:
In [5]:
dfmin = df.pivot_table(index='Date', columns='Element', aggfunc='min')
dfmin.drop('ID', 1, inplace=True)
dfmin.columns = dfmin.columns.droplevel()
dfmin.drop('TMAX', 1, inplace=True)
dfmin.head()
Out[5]:
In [6]:
# 2015 Record Data
df3 = pd.concat([dfmax, dfmin], axis=1)
df2015 = df3[3652:]
df2015['TMAX'] = df2015['TMAX']/10
df2015['TMIN'] = df2015['TMIN']/10
df2015['TMAX15'] = df2015['TMAX']
df2015['TMIN15'] = df2015['TMIN']
df2015.drop('TMAX', 1, inplace=True)
df2015.drop('TMIN', 1, inplace=True)
df2015 = df2015.reset_index()
df2015.head()
Out[6]:
In [7]:
import datetime
dfhist = df3[:-365]
dfhist = dfhist.reset_index()
dfhist['Date'] = dfhist['Date'].apply(lambda x: x.strftime('%m-%d-00'))
dfhist['Date'] = pd.to_datetime(dfhist['Date'])
# Max
dfhistmax = dfhist.drop('TMIN', 1)
dfhistmax = dfhistmax.pivot_table(values='TMAX', index=['Date'], aggfunc='max')
dfhistmax = dfhistmax.to_frame()
# Min
dfhistmin = dfhist.drop('TMAX', 1)
dfhistmin = dfhistmin.pivot_table(values='TMIN', index=['Date'], aggfunc='min')
dfhistmin = dfhistmin.to_frame()
print(dfhistmin.head())
print(dfhistmax.head())
In [8]:
dfhistc = pd.concat([dfhistmax, dfhistmin], axis=1)
dfhistc = dfhistc.reset_index()
dfhistc['Date'] = dfhistc['Date'].apply(lambda x: x.strftime('%m-%d'))
dfhistc.set_index('Date', inplace=True)
dfhistc.drop(dfhistc.index[59], inplace=True)
dfhistc = dfhistc.reset_index()
dfhistc['TMAX'] = dfhistc['TMAX']/10
dfhistc['TMIN'] = dfhistc['TMIN']/10
dfhistc.info()
In [9]:
# Merge historical and 2015 dfs
dfhistc = pd.concat([dfhistc, df2015], axis=1)
#dfhistc['2015Max'] = "NaN"
dfhistc['2015Max'] = np.where((dfhistc['TMAX15'] > dfhistc['TMAX']), dfhistc['TMAX15'], np.nan)
dfhistc['2015Min'] = np.where((dfhistc['TMIN15'] < dfhistc['TMIN']), dfhistc['TMIN15'], np.nan)
dfhistc.head()
Out[9]:
In [10]:
%matplotlib notebook
plt.figure()
# plot the linear data and the exponential data
plt.plot(dfhistc['TMAX'], '-r', dfhistc['TMIN'], '-b')
# Fill
plt.gca().fill_between(range(len(dfhistc)),
dfhistc['TMAX'], dfhistc['TMIN'],
facecolor='blue',
alpha=0.20)
# plot points where 2015 broke records
plt.plot(dfhistc['2015Max'], 'm^', dfhistc['2015Min'], 'c^', ms=6.0)
# Beautify & Lables
ax = plt.gca()
ax.set_xlabel('Days of the Year', alpha=0.7)
ax.set_ylabel('degrees celsius', alpha=0.7)
ax.set_title('Historical Max. & Min. Temperatures Overlayed with 2015 Records', alpha=0.7)
# Remove ticks
plt.tick_params(top='off', bottom='off', left='off', right='off')
# remove the frame of the chart
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)